<template>
  <div class="loginBody">
    <div class="loginDiv">
      <h1 class="loginTitle">学生在线学习系统</h1>
      <div class="loginContent">
        <div class="loginImg">
          <img
            style="width: 380px; height: 300px"
            src="../../assets/learn.jpeg"
          />
        </div>
        <!-- 登录表单区域 -->
        <el-form :model="tabUser" label-width="100px" size="medium ">
          <!-- 用户名 -->
          <el-form-item prop="account">
            <el-input
              v-model="tabUser.account"
              placeholder="账号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="tabUser.password"
              placeholder="密码"
              prefix-icon="el-icon-user"
              show-password
              @keyup.enter.native="confirm"
            ></el-input>
          </el-form-item>
          <!-- 记住我 -->
          <el-form-item>
            <el-checkbox
              label="记住我"
              class="rememberMe"
              v-model="tabUser.checkbox"
            ></el-checkbox>
          </el-form-item>
          <!-- 登录注册按钮 -->
          <el-button
            style="margin-left: 100px"
            type="success"
            @click="login(tabUser)"
            >登录</el-button
          >
          <el-button
            style="margin-left: 25px"
            type="primary"
            @click="dialog = true"
            >注册</el-button
          >
        </el-form>
      </div>
    </div>
    <!--    侧栏注册-->
    <el-drawer
      ref="drawer"
      size="50%"
      :visible.sync="dialog"
      custom-class="demo-drawer"
      title="学生在线学习系统 -- 注册页"
    >
      <el-form :model="form" :rules="rules">
        <el-form-item>
          <el-input
            v-model="form.userName"
            placeholder="姓名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.account"
            placeholder="账号"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.phone"
            placeholder="电话"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item :show-password="true" prop="password">
          <el-input
            v-model="form.password"
            :placeholder="form.password"
            placeholder="密码"
            style="width: 500px"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item :show-password="true" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            :placeholder="form.confirmPassword"
            placeholder="确认密码"
            style="width: 500px"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option label="教师" value="2"></el-option>
            <el-option label="学生" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <el-button
          style="margin-left: 120px"
          type="primary"
          @click="confirm(form)"
          >确定</el-button
        >
        <el-button style="margin-left: 60px" @click="cancelForm"
          >取 消</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { loginRequest, VerificationCode, register } from "../../api/login";
import Cookies from "js-cookie";
import form from "element-ui/packages/form";
export default {
  name: "Login",
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      isShowSlide: false,
      tabUser: {
        account: "",
        password: "",
        // VerificationCode: '',
        checkbox: false,
      },
      form: {
        userName: "",
        account: "",
        sex: "",
        phone: "",
        password: "",
        confirmPassword: [],
        roleId: "",
        desc: "",
      },
      formLabelWidth: "80px",
      timer: null,
      dialog: false,
      loading: false,

      rules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern:
              /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{6,13}$/,
            message:
              "密码为数字字母，特殊符号 至少包含三种，长度为 8 - 30位,不区分大小写",
          },
        ],
        confirmPassword: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    //登录验证
    hideSlide() {
      setTimeout(() => {
        this.isShowSlide = false;
      }, 500);
    },
    confirm(form) {
      register(form).then((resp) => {
        if (resp.data.code == 200) {
          this.dialog = false;
          this.$message({
            message: "恭喜你，注册成功",
            type: "success",
          });
        } else {
          this.$message.error("注册失败");
        }
      });
    },
    cancelForm() {
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },

    sendCode() {
      VerificationCode().then((resp) => {
        console.log("123");
      });
    },
    login(tabUser) {
      loginRequest(tabUser).then((resp) => {
        Cookies.set("roleId", resp.data.resultData.roleId);
        Cookies.set("userId", resp.data.resultData.userId);
        Cookies.set("classId", resp.data.resultData.classId);

        if (resp.data.code == 200) {
          if (
            resp.data.resultData.roleId == 1 ||
            resp.data.resultData.roleId == 4 ||
            resp.data.resultData.roleId == 5
          ) {
            this.$router.push("/adminmanagement");
          }
          if (resp.data.resultData.roleId == 2) {
            this.$router.push("/hometeacher");
          }
          if (resp.data.resultData.roleId == 3) {
            this.$router.push("/studentweb");
          }
          this.$message({
            message: "恭喜你，登录成功",
            type: "success",
          });
        } else {
          this.$message.error("账号或者密码错误");
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("../../images/01.jpg");
  background-size: 100%;
  margin: 0;
}

.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -400px;
  width: 800px;
  height: 450px;
  background-color: whitesmoke;
  border-radius: 5%;
  opacity: 0.9;
}

.loginTitle {
  margin: 20px 0;
  text-align: center;
}
.loginImg {
  width: 300px;
  height: 300px;
}
.loginContent {
  width: 700px;
  height: 300px;
  position: absolute;
  top: 100px;
  left: 45px;
  display: flex;
  justify-content: space-between;
}
/deep/.el-drawer {
  &.rtl {
    &.demo-drawer {
      background-image: url("../../images/02.jpg");
      background-size: 100% 100%;
      opacity: 0.8;
    }
  }
  .el-drawer__header {
    color: #ffffff;
    font-size: 22px;
    position: center;
    margin-left: 135px;
    margin-right: 120px;
  }
  .el-drawer__body {
    //width: 500px;
    margin-left: 120px;
    margin-right: 120px;
    position: center;
    /deep/.el-form-item {
      width: 100px;
      &.is-required {
      }
    }
  }
}
</style>
